﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using CNKI.TPI.Web.Base.Log;
using CNKI.TPI.Web.Search.Model;
using System.Web.SessionState;
using System.Web.Http.Controllers;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Log4net.config", Watch = true)]
namespace CNKI.TPI.Web.Base
{
    public class LogOpr
    {
        public enum LogLevel
        {
            Debug,
            Info,
            Warn,
            Error,
            Fatal,
        };

        private static ILog logger = log4net.LogManager.GetLogger("SysLogger");
        private static ILog downloadLogger = log4net.LogManager.GetLogger("DownloadLogger");
        private static ILog browseLogger = log4net.LogManager.GetLogger("BrowseLogLogger");
        private static ILog searchLogger = log4net.LogManager.GetLogger("SearchLogLogger");
        private static ILog loginLogger = log4net.LogManager.GetLogger("LoginLogger");

        public static void Debug(int type, long elapsed, string operateClass, string operateFunc, string inParameter, string outParameter, string message, string Ip = null)
        {
            if (Ip == null)
            {
                Ip = IPUntil.GetIP();
            }
            Output(type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, LogLevel.Debug, Ip);
        }

        public static void Debug(string message)
        {
            Debug(2, 0, "", "", "", "", message,"");
        }

        public static void Info(int type, long elapsed, string operateClass, string operateFunc, string inParameter, string outParameter, string message, string Ip = null)
        {
            if (Ip == null)
            {
                Ip = IPUntil.GetIP();
            }
            Output(type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, LogLevel.Info, Ip);
        }

        public static void Info(string message)
        {
            Info(2, 0, "", "", "", "", message, "");
        }

        public static void Warn(int type, long elapsed, string operateClass, string operateFunc, string inParameter, string outParameter, string message, string Ip = null)
        {
            if (Ip == null)
            {
                Ip = IPUntil.GetIP();
            }
            Output(type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, LogLevel.Warn, Ip);
        }

        public static void Warn(string message)
        {
            Warn(2, 0, "", "", "", "", message, "");
        }

        public static void Error(int type, long elapsed, string operateClass, string operateFunc, string inParameter, string outParameter, string message, string Ip = null)
        {
            if (Ip == null)
            {
                Ip = IPUntil.GetIP();
            }
            Output(type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, LogLevel.Error, Ip);
        }

        public static void Error(string message)
        {
            Error(2, 0, "", "", "", "", message, "");
        }

        public static void Fatal(int type, long elapsed, string operateClass, string operateFunc, string inParameter, string outParameter, string message, string Ip = null)
        {
            if (Ip == null)
            {
                Ip = IPUntil.GetIP();
            }
            Output(type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, LogLevel.Fatal, Ip);
        }

        public static void Fatal(string message)
        {
            Fatal(2, 0, "", "", "", "", message, "");
        }

        private static void Output(int type, long elapsed, string operateClass, string operateFunc, string inParameter, string outParameter, string message, LogLevel loglevel, string Ip)
        {
            //判断日志是否打开
            if (!IsLogEnabled(loglevel))
            {
                return;
            }

            //获得登录信息
            string userid = "0";
            string userName = "anonymous";
            //HttpSessionState session = null;
            //if (System.Web.HttpContext.Current != null)
            //{
            //    session = System.Web.HttpContext.Current.Session;
            //}
                
            //if (null != session)
            //{
            //    var userInfo = session["UserInfo"];
            //    if (null != userInfo)
            //    {
            //        userid = ((LoginUserInfo)userInfo).UserID;
            //        userName = ((LoginUserInfo)userInfo).UserCode;
            //    }
            //}

            if (SessionHelper.IsUserInfoExsit())
            {
                var userInfo = SessionHelper.GetUserInfo();
                if (null != userInfo)
                {
                    userid = ((LoginUserInfo)userInfo).UserID;
                    userName = ((LoginUserInfo)userInfo).UserCode;
                }
            }

            //输出日志
            switch (loglevel)
            {
                case LogLevel.Debug:
                    logger.Debug(new LogMessage(userid, userName, type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, Ip));
                    break;
                case LogLevel.Info:
                    logger.Info(new LogMessage(userid, userName, type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, Ip));
                    break;
                case LogLevel.Warn:
                    logger.Warn(new LogMessage(userid, userName, type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, Ip));
                    break;
                case LogLevel.Error:
                    logger.Error(new LogMessage(userid, userName, type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, Ip));
                    break;
                case LogLevel.Fatal:
                    logger.Fatal(new LogMessage(userid, userName, type, elapsed, operateClass, operateFunc, inParameter, outParameter, message, Ip));
                    break;
            }
        }

        public static bool IsLogEnabled(LogLevel loglevel)
        {
            bool result = false;
            switch (loglevel)
            {
                case LogLevel.Debug:
                    result = logger.IsDebugEnabled;
                    break;
                case LogLevel.Info:
                    result = logger.IsInfoEnabled;
                    break;
                case LogLevel.Warn:
                    result = logger.IsWarnEnabled;
                    break;
                case LogLevel.Error:
                    result = logger.IsErrorEnabled;
                    break;
                case LogLevel.Fatal:
                    result = logger.IsFatalEnabled;
                    break;
            }
            return result;
        }

        public static void Download(string dbID, string dbCode, string recordID, string fileID, int source, string title)
        { 
            //获得登录信息
            string userid = "0";
            string userName = "anonymous";

            if (SessionHelper.IsUserInfoExsit())
            {
                var userInfo = SessionHelper.GetUserInfo();
                if (null != userInfo)
                {
                    userid = ((LoginUserInfo)userInfo).UserID;
                    userName = ((LoginUserInfo)userInfo).UserCode;
                }
            }
            downloadLogger.Info(new LogMessage(userid, userName, IPUntil.GetIP(), dbID, dbCode, recordID, fileID, source, title));
        }

        public static void Download(string dbID, string dbCode, string recordID, string fileID, int source, string userid, string usercode)
        {
            downloadLogger.Info(new LogMessage(userid, usercode, IPUntil.GetIP(), dbID, dbCode, recordID, fileID, source, ""));
        }

        public static void Browse(string dbID, string dbCode, string recordID, int source, string title)
        {
            //获得登录信息
            string userid = "0";
            string userName = "anonymous";

            if (SessionHelper.IsUserInfoExsit())
            {
                var userInfo = SessionHelper.GetUserInfo();
                if (null != userInfo)
                {
                    userid = ((LoginUserInfo)userInfo).UserID;
                    userName = ((LoginUserInfo)userInfo).UserCode;
                }
            }
            browseLogger.Info(new LogMessage(userid, userName, IPUntil.GetIP(), dbID, dbCode, recordID, source, title));
        }
        public static void Browse(string dbID, string dbCode, string recordID, int source, string userid, string usercode)
        {
            browseLogger.Info(new LogMessage(userid, usercode, IPUntil.GetIP(), dbID, dbCode, recordID, source, ""));
        }

        public static void Login(string userName, string status)
        {
            //获得登录信息
            loginLogger.Info(new LogMessage(userName, IPUntil.GetIP(), status));
        }

        public static void Search(string dbID, string dbCode, string query, string order, string searchType, string classCode, string topicCLS, string topicNode)
        {
            //获得登录信息
            string userid = "0";
            string userName = "anonymous";

            if (SessionHelper.IsUserInfoExsit())
            {
                var userInfo = SessionHelper.GetUserInfo();
                if (null != userInfo)
                {
                    userid = ((LoginUserInfo)userInfo).UserID;
                    userName = ((LoginUserInfo)userInfo).UserCode;
                }
            }
            searchLogger.Info(new LogMessage(userid, userName, IPUntil.GetIP(), dbID, dbCode, query, order, searchType, classCode, topicCLS, topicNode));
        }
        public static void Search(string dbID, string dbCode, string query, string order, string searchType, string classCode, string userid, string usercode, string topicCLS, string topicNode)
        {
            searchLogger.Info(new LogMessage(userid, usercode, IPUntil.GetIP(), dbID, dbCode, query, order, searchType, classCode, topicCLS, topicNode));
        }
    }
}
